package ShapeParsingAndAnalysis;

import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ShapeParser {
    public ArrayList<Shape> parseFile(String filePath) {
        ArrayList<Shape> shapes = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split(",");
                if (parts.length == 0) continue;
                switch (parts[0].trim().toLowerCase()) {
                    case "circle":
                        if (parts.length == 2) shapes.add(new Circle(Double.parseDouble(parts[1])));
                        break;
                    case "square":
                        if (parts.length == 2) shapes.add(new Square(Double.parseDouble(parts[1])));
                        break;
                    case "rectangle":
                        if (parts.length == 3) shapes.add(new Rectangle(Double.parseDouble(parts[1]), Double.parseDouble(parts[2])));
                        break;
                    case "triangle":
                        if (parts.length == 4) shapes.add(new Triangle(Double.parseDouble(parts[1]), Double.parseDouble(parts[2]), Double.parseDouble(parts[3])));
                        break;
                }
            }
        } catch (IOException e) {
            System.err.println("无法读取文件: " + filePath);
        } catch (NumberFormatException e) {
            System.err.println("文件格式错误: " + e.getMessage());
        }
        return shapes;
    }
}